package com.smz.service.impl;

import com.smz.dao.TCargoDAO;
import com.smz.dao.TIoWarehouseDAO;
import com.smz.dao.TProductHandoverDAO;
import com.smz.entity.PageEntity;
import com.smz.entity.TCargo;
import com.smz.entity.TIoWarehouse;
import com.smz.entity.TProductHandover;
import com.smz.service.TProductHandoverService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TProductHandoverServiceImpl implements TProductHandoverService {
    @Autowired
    private TProductHandoverDAO tProductHandoverDAO;
    @Autowired
    private TCargoDAO tCargoDAO;
    @Autowired
    private TIoWarehouseDAO tIoWarehouseDAO;

    @Override
    public PageEntity<TProductHandover> findPageProHandover(@Param("start") int start,
                                                            @Param("length") int length,
                                                            @Param("orderId") String orderId,
                                                            @Param("time") String time) {
        int count = Math.toIntExact(tProductHandoverDAO.selectCount(null));
        System.out.println(count);

//
        int pageCount = (count) % (start*length) == 0 ? (count) / (start*length) : (count) % (start*length) +1;
        if (start-1<0){
            start=0;
        }
        List<TProductHandover> tProductHandoverPageEntity = tProductHandoverDAO.selectPageProHandover((start-1)*length,length,orderId,time);

        return new PageEntity<>(tProductHandoverPageEntity,start,tProductHandoverPageEntity.size(),length);
    }

    @Override
    public void AddProHandover(TProductHandover tProductHandover) {
        tProductHandoverDAO.insertProHandover(tProductHandover);
    }

    @Override
    public void removeProHandoverById(Integer id) {
        tProductHandoverDAO.deleteProHandoverById(id);
    }

    @Override
    public void modifyProHandoverById(TProductHandover tProductHandover) {
        tProductHandoverDAO.updateProHandoverById(tProductHandover);
    }

    @Override
    public void modifyProOrderState(Integer id) {
        tProductHandoverDAO.updateProOrderState(id);
    }

    @Override
    public TProductHandover findProHandoverByOrderId(Integer orderId) {
        return tProductHandoverDAO.selectProHandoverByOrderId(orderId);
    }


    /**
     * @Authoe: smz
     * @Description: 查询所有交接单
     * @Date: 16:54 2021-11-25
     */
    @Override
    public List<TProductHandover> findProHandovers() {
        return tProductHandoverDAO.selectProHandovers();
    }


    /**
     * @Authoe: smz
     * @Description: 更改交接单状态，新增库存，新增出入库表状态
     * @Date: 9:47 2021-11-30
     */
    @Override
    @Transactional
    public void addAndUpdateTIoWareHouse(TProductHandover tProductHandover,String menuId,String reservoirId,String shelvesId,
                                         String cargoId,String ioNo,String batch,String ioTime,String ioType) {
        //修改交接表状态
        tProductHandoverDAO.updateProOrderState(tProductHandover.getId());
        //通过仓库，库区，货架，货位精准查询到一个
        TCargo tCargoByMenuIdAndMore = tCargoDAO.findTCargoByMenuIdAndMore(menuId, reservoirId, shelvesId, cargoId);
        if (tCargoByMenuIdAndMore.getProduct().equals("")){
            tCargoByMenuIdAndMore.setNum(Integer.parseInt(tProductHandover.getNum()));
            tCargoByMenuIdAndMore.setStatus("1");
            tCargoByMenuIdAndMore.setBatch(batch);
            tCargoByMenuIdAndMore.setProduct(tProductHandover.getPhoAndC().getPName());
            //修改货位数据
            tCargoDAO.updateById(tCargoByMenuIdAndMore);
        }else {
            //把入库数量跟库存数量进行相加
            int num = tCargoByMenuIdAndMore.getNum()+Integer.parseInt(tProductHandover.getNum());
            tCargoByMenuIdAndMore.setNum(num);
            //修改货位数据
            tCargoDAO.updateById(tCargoByMenuIdAndMore);
        }
        //封装出入库实体类对象
        TIoWarehouse tIoWarehouse = new TIoWarehouse(0,ioNo,ioType,Integer.parseInt(batch),tProductHandover.getPhoAndC().getPName(),
                Integer.parseInt(tProductHandover.getNum()),tProductHandover.getMenu().getWareName(),ioTime);
        //新增数据
        tIoWarehouseDAO.insert(tIoWarehouse);
    }
}
